package pri.black.dogview.util;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.request.ServletWebRequest;

public class ServletContextUtil {
    private static HttpServletRequest httpServletRequest(){
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes instanceof ServletRequestAttributes servletRequestAttributes) {
            HttpServletRequest request = servletRequestAttributes.getRequest();
            return request;
        }

        return null;
    }

    public static String getIP(){
        String remoteAddr = httpServletRequest().getRemoteAddr();
        if (remoteAddr.equals("127.0.0.1")) {
            remoteAddr = httpServletRequest().getHeader("X-Real-IP");
        }
        return remoteAddr;
    }
}
